package com.huidao.servlet.buyer;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.huidao.entity.Goods;
import com.huidao.entity.Order;
import com.huidao.entity.User;
import com.huidao.service.OrderService;
import com.huidao.service.OrderServiceImpl;

/**
 * Servlet implementation class BuyerOrderAddServlet
 */
public class BuyerOrderAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyerOrderAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Order order = new Order();
		Goods g = new Goods();
		g.setGoodsId(Long.parseLong(request.getParameter("id")));
		order.setGoods(g);
		order.setGoodsNum(Integer.parseInt(request.getParameter("num")));
		User user = (User) request.getSession().getAttribute("user");
		order.setUser(user);
		String sid = request.getParameter("sid");
		
		OrderService se = new OrderServiceImpl();
		int state = se.addOrder(order,sid);
		if(state==0){
			response.getWriter().print("库存不足");
			response.setHeader("refresh", "2;url=BuyerGoodsListServlet");
		}else if(state==2){
			response.getWriter().print("添加失败");
			response.setHeader("refresh", "2;url=BuyerGoodsListServlet");
		}else if(state==1){
			response.sendRedirect("BuyerOrderListServlet");
		}else if(state==4){
			response.getWriter().print("购物车失败");
			response.setHeader("refresh", "2;url=BuyerShopListServlet");
		}else{
			response.getWriter().print("数据库连接异常");
			response.setHeader("refresh", "2;url=BuyerGoodsListServlet");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
